package org.autumn.structure.linked;

/**
 * 双向链表
 */
public class DoubleLinked<T> {

    private Node head;

    public void add(T t) {
        Node addition = new Node(t, null, null);
        if (head == null) {
            head = addition;
        } else {
            // 找到下一个节点为空的节点,讲新增的挂载上去
            Node node = head;
            while (node.next != null) {
                node = node.next;
            }
            addition.setPre(node);
            node.next = addition;
        }
    }

    // 节点
    class Node {
        private Object data;
        private Node pre;
        private Node next;

        public Node() {
        }

        public Node(Object data, Node pre, Node next) {
            this.data = data;
            this.pre = pre;
            this.next = next;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }

        public Node getPre() {
            return pre;
        }

        public void setPre(Node pre) {
            this.pre = pre;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

}
